package com.service.impl;

import com.dao.SysMenuDao;
import com.dao.SysRoleMenuDao;
import com.domain.Node;
import com.domain.sys.SysMenu;
import com.exception.ServiceException;
import com.service.SysMenuService;
import com.util.MyValid;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuDao sysMenuDao;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;
    @Override
    public ArrayList<HashMap<String, Object>> findObject() {
        //...
        return sysMenuDao.findObject();
    }

    @Override
    public Integer deleteObject(Integer id) {
        MyValid.isIll(id == null || id < 0,"id值无效");


        int childCount = sysMenuDao.getChildCount(id);

        MyValid.isService(childCount > 0,"请先删除子菜单");


        sysRoleMenuDao.deleteObjectByMenuId(id);
        int row = sysMenuDao.deleteObject(id);
        MyValid.isService(row == 0,"记录不见啦呦~");


        return row;
    }

    @Override
    public ArrayList<Node> findZtreeMenuNodes() {
        return sysMenuDao.findZtreeMenuNodes();
    }

    @Override
    public Integer insertObject(SysMenu entity) {
        if(entity == null)
            throw new IllegalArgumentException("保存对象不能为空");
        if(StringUtils.isEmpty(entity.getName()))
            throw new IllegalArgumentException("菜单名不能为空");


        return sysMenuDao.insertObject(entity);
    }

    @Override
    public Integer updateObject(SysMenu sysMenu) {

        if(sysMenu == null)
            throw new IllegalArgumentException("保存对象不能为空");
        if(StringUtils.isEmpty(sysMenu.getName()))
            throw new ServiceException("菜单名不能为空");


        return sysMenuDao.updateObject(sysMenu);
    }

}
